/**
 * 
 */
var oprater = {

	edit : function(id) {
		layer_show_max('编辑', contextPath+'/sys/menu/goEdit/' + id);
	},
	add : function(id) {
		layer_show_max('添加', contextPath+'/sys/menu/goAdd');
	},
	del : function(id){
		if(!id){
			return;
		}
		layer.confirm('确定要删除该菜单？',function(){		
			$.ajax({
				url:contextPath+'/sys/menu/del',
				data:$('#form-model').serialize(),
				type:'post',
				dataType:'json',
				data:{id:id},
				success:function(data){
					if(data.result){										
						layer.msg('删除成功！',{
						    time: 2000, //2s后自动关闭					  
						});
						oTable.draw();
					}else{
						layer.msg(data.msg,{
						    time: 2000, //2s后自动关闭					  
						});	
					}							
				},
				error:function(){
					layer.alert('删除失败，请联系管理员！');				
				}
			});	
		});	
	},
	changeActive : function(id, active) {
		var msg = "确认要停用吗？";
		if (active == 1) {
			msg = "确认要启用吗？";
		}
		layer.confirm(msg, function(index) {
			$.ajax({
				url : contextPath+'/sys/menu/save',
				data : {
					'id' : id,
					'active' : active
				},
				dataType : 'json',
				method : 'post',
				success : function(data) {
					if (active == 1) {
						layer.msg('成功启用!', {
							icon : 6,
							time : 2000
						});
					} else {
						layer.msg('成功停用!', {
							icon : 5,
							time : 2000
						});
					}
					oTable.draw();
				}
			});
		});
	},
	makeTree : function(url,divId,callback){	
		var setting = {
	            data: {
                    simpleData: {
                            enable: true,
                            idKey: "id",
                            pIdKey: "pId",
                            rootPId: "0"
                    }
	            },
	            async: { 
	                    type: "post",
	                    enable: true,
	                    url: url,
	            },
	            callback : {
	                onClick: callback              
	            }
	    };
	    $.fn.zTree.init($("#"+divId), setting);//初始化树对象a
	}
}

var oTable;
$(function() {
	
	oprater.makeTree(contextPath+'/sys/menu/loadAllMenu','menulist',function(event,treeId,node){
		$('#pId').val(node.id);
		oTable.draw();
	});
	
	oTable = $('#table').tjTable({
		select:{
			type:'default',
			name:''
		},
		ajax : {
			url  : contextPath+"/sys/menu/loadPage",
			type : "post",
			data : {
				'filters' : function() {
					var vo = [];
					var name = $('#name').val();
					var pId = $('#pId').val();
					if (name != null && name != "")
						vo.push({
							name : 'name',
							oprate:'like',
							value : '%'+name+'%'
						});
					if (pId != null && pId != "")
						vo.push({
							name : 'pId',
							oprate:'=',
							value : pId
						});
					return JSON.stringify(vo);
				}
			}
		},
		columns : [
				{
					"sTitle" : "图标",
					"data" : "icons",
					"render" : function(data, type, row) {
						if(row.icons!=null&&row.icons!=''){
							return "<i class=\"Hui-iconfont\">"+row.icons+"</i>";
						}
						else{
							return row.icons;
						}
					}
				},
				{
					"sTitle" : "菜单名称",
					"data" : "name"
				},
				{
					"sTitle" : "链接",
					"data" : "url"
				},
				{
					"sTitle" : "父菜单",
					"data" : "pName",
					"render" : function(data, type, row) {
						if(row.pName){
							return row.pName;
						}else{
							return "--";
						}
					}
				},								
				{
					"sTitle" : "顺序",
					"data" : "seq"
				},								
				{
					"sTitle" : "状态",
					"data" : "active",
					"render" : function(data, type, row) {
						if (row.active == 1) {
							return "<span class=\"label label-success radius\">启用</span>";
						} else {
							return "<span class=\"label label-defaunt radius\">停用</span>";
						}
					}
				},
				{
					"sTitle" : "操作",
					"render" : function(data, type, row) {		
						
						var str = "<a href='javascript:oprater.del(\""
							+ row.id
							+ "\")' title='删除'><i class=\"Hui-iconfont\">&#xe6a1;</i>删除</a>&nbsp;&nbsp;";
						str = str
						+ "<a href='javascript:oprater.edit(\""
						+ row.id
						+ "\")' title='编辑'><i class=\"Hui-iconfont\">&#xe6df;</i>编辑</a>&nbsp;&nbsp;";
						if (row.active == 1) {
							str = str
									+ "<a href='javascript:oprater.changeActive(\""
									+ row.id
									+ "\",0)' title='停用'><i class=\"Hui-iconfont\">&#xe631;</i>停用</a>&nbsp;&nbsp;";
						} else {
							str = str
									+ "<a href='javascript:oprater.changeActive(\""
									+ row.id
									+ "\",1)' title='启用'><i class=\"Hui-iconfont\">&#xe615;</i>启用</a>&nbsp;&nbsp;";
						}
						return str;
					}
				} ]
	});

	$('#search').click(function() {
		oTable.draw();
	});

	$('#add-btn').click(function() {
		oprater.add();
	});
});